﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class Js_xheditor_zh_cn_upload : System.Web.UI.Page
{
    private const string ALLOW_FILE_EXT = "|JPG|GIF|BMP|JPEG|SWF|WMV|MPEG|RAR|ZIP|DOC|XLS|PPT|DOCX|XLSX|PPTX|";

    protected void Page_Load(object sender, EventArgs e)
    {
       

        if (Request.Files.Count == 0 || Request.Files[0].ContentLength == 0)
        {
            Response.Write("{\"err\":\"没有要上传的文件\",\"msg\":\"\"}");
            return;
        }

        HttpPostedFile file = Request.Files[0];
        string filename = file.FileName;
        string filext = filename.Substring(filename.LastIndexOf(".") + 1);

        if (!ALLOW_FILE_EXT.Contains("|" + filext.ToUpper() + "|"))
        {
            Response.Write("{\"err\":\"不允许上传的文件类型\",\"msg\":\"\"}");
            return;
        }

        DateTime now = DateTime.Now;

        string tofile = Server.MapPath("~") + "\\EditorUpload\\" + now.ToString("yyyy") + "\\" + now.ToString("MM") + "\\" + now.ToString("ddHHmmss") + "." + filext;
        try
        {
            if (!Directory.Exists(Path.GetDirectoryName(tofile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(tofile));
            }

            file.SaveAs(tofile);
        }
        catch
        {
            Response.Write("{\"err\":\"上传文件出错\",\"msg\":\"\"}");
            return;
        }

        Response.Write("{\"err\":\"\",\"msg\":\"" + Request.Url.ToString().Replace(Request.RawUrl, "") +
                       Request.ApplicationPath + tofile.Replace(Server.MapPath("~"), "").Replace("\\", "/") + "\"}");
    }
}
